﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;

namespace PhilWicklund.com.FacebookParts
{
    public class StatusPart: System.Web.UI.WebControls.WebParts.WebPart
    {
        private bool showProfilePicture = true;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(false),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Show Profile Picture"),
         System.Web.UI.WebControls.WebParts.WebDescription("If True, the profile picture of the user will show to the left of their status."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(true)
        ]
        public bool ShowProfilePicture
        {
            get
            {
                return showProfilePicture;
            }
            set
            {
                showProfilePicture = value;
            }
        }

        private bool useLoggedInUser = true;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(false),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Use Logged in User"),
         System.Web.UI.WebControls.WebParts.WebDescription("If True, the status of the logged in user will show. If false, set the User ID property."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(true)
        ]
        public bool UseLoggedInUser
        {
            get
            {
                return useLoggedInUser;
            }
            set
            {
                useLoggedInUser = value;
            }
        }

        private int userId = -1;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(false),
         System.Web.UI.WebControls.WebParts.WebDisplayName("User Id"),
         System.Web.UI.WebControls.WebParts.WebDescription("Enter the user ID (64bit integer) of the facebook account to retrieve status from. If the UseLoggedInUser property is set to True, this property will not be used."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(-1)
        ]
        public int UserId
        {
            get
            {
                return userId;
            }
            set
            {
                userId = value;
            }
        }

        private Sizes imageSize = Sizes.thumb;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(false),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Image Size"),
         System.Web.UI.WebControls.WebParts.WebDescription("Select how big you want the profile image to show."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(Sizes.thumb)
        ]
        public Sizes ImageSize
        {
            get
            {
                return imageSize;
            }
            set
            {
                imageSize = value;
            }
        }

        private Orientations orientation = Orientations.left;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(false),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Orientation"),
         System.Web.UI.WebControls.WebParts.WebDescription("Select the centering for the web part."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(Orientations.left)
        ]
        public Orientations Orientation
        {
            get
            {
                return orientation;
            }
            set
            {
                orientation = value;
            }
        }

        public override EditorPartCollection CreateEditorParts()
        {
            List<EditorPart> editorParts = new List<EditorPart>(1);
            EditorPart part = new StatusEditorPart();
            part.Title = "Status Part Settings";
            part.ID = this.ID + "_StatusEditorPart";
            editorParts.Add(part);
            EditorPartCollection baseparts = base.CreateEditorParts();
            return new EditorPartCollection(baseparts, editorParts);
        }

        protected override void CreateChildControls()
        {
            try
            {
                StringBuilder script = new StringBuilder();

                string id;
                //if (UseLoggedInUser)
                //    id = "loggedinuser";
                //else
                //{
                if (UserId == -1)
                {
                    this.Controls.Add(new LiteralControl("The user Id property has not yet been specified, <a href=\"javascript:MSOTlPn_ShowToolPane2Wrapper('Edit', '16', '" + this.ID + "')\">open the tool pane</a> to set the necessesary properties."));
                    return;
                }
                else
                {
                    id = UserId.ToString();
                }
                //}

                if (ShowProfilePicture)
                {
                    script.Append("<table width='100%'><tr><td width='100%' align='" + Orientation.ToString() + "'><table style='padding:5px'><tr><td valign=\"middle\"><fb:profile-pic uid=\"" + id + "\" linked=\"true\" size=\"" + ImageSize.ToString() + "\"></fb:profile-pic></td><td></td>")
                        .Append("<td><i><fb:name uid=\"" + id + "\" />&nbsp;<fb:user-status uid=\"" + id + "\"></fb:user-status></i></td></tr></table></td></tr></table>");
                }
                else
                {
                    script.Append("<table style='padding:5px;' width='100%'><tr><td align='" + Orientation.ToString() + "'><fb:name uid=\"" + id + "\" />&nbsp;<fb:user-status uid=\"" + id + "\"></fb:user-status></td></tr></table>");
                }
                this.Controls.Add(new LiteralControl(script.ToString()));
            }
            catch (Exception e) { this.Controls.Add(new LiteralControl(e.Message)); }
        }
    }
}
